<%--
  Created by IntelliJ IDEA.
  User: 86135
  Date: 2023/3/31
  Time: 21:06
  To change this template use File | Settings | File Templates.
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>进货管理</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 引入 Bootstrap -->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body{
            background-image: url("${pageContext.request.contextPath}/statics/images/bg.png");
        }
    </style>
    <script type="text/javascript">
        function startTurn(){
            var turnPage = document.getElementById("turnPage").value;
            if(turnPage > ${page.pageCount}){
                alert("超过最大页数，请重新输入！");
                return false;
            }
            if(turnPage < 1){
                alert("低于最小页数，请重新输入！");
                return false;
            }
            var shref="${pageContext.request.contextPath}/admin/toStocking?pageNo="+turnPage;
            window.location.href=shref;
        }
    </script>
    <script src="${pageContext.request.contextPath}/statics/js/jquery-3.6.0.js"></script>
    <script type="text/javascript">
        function p(){
            var bookISBN = document.getElementsByClassName("bookISBN");
            var bookName = document.getElementsByClassName("bookName");
            var addr = document.getElementsByClassName("addr");
            var nums = document.getElementsByClassName("nums");
            var list = [];
            for (i = 0; i < bookISBN.length; i++) {
                list.push({
                    bookISBN: bookISBN[i].value,
                    bookName: bookName[i].innerHTML,
                    counts: nums[i].value,
                    address: addr[i].value
                })
                console.log(list[i]);
            }
            $.ajax({
                cache: true,
                type: "POST",
                url: '${pageContext.request.contextPath}/admin/updateStocking',
                // 指定请求的数据格式为json，实际上传的是json字符串
                data: JSON.stringify(list),
                //指定请求的数据格式为json,这样后台才能用@RequestBody 接受java bean
                contentType: 'application/json;charset=utf-8',
                async: false,
                success: function (data,status){
                    if (data == "success"){
                        alert("进货成功！")
                        window.location.href="${pageContext.request.contextPath}/admin/toStocking"
                    } else {
                        alert("进货失败！")
                        history.back()
                    }
                },
                error : function(data,status) {
                    alert("数据上传失败: "+status);
                }
            });
        }
    </script>
</head>

<body>

<div class="container">

    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="page-header">
                <h1>
                    <small>进货管理</small>
                    <button class="btn btn-primary" onclick="window.location = '${pageContext.request.contextPath}/admin/index' " style="float: right">返回</button>
                    <a href="${pageContext.request.contextPath}/admin/displayStocking">进货记录</a>
                </h1>
            </div>
        </div>
    </div>

    <div class="row clearfix">
        <div class="col-md-12 column">
            <table class="table table-hover table-striped">
                <thead>
                <tr>
                    <th>书籍图片</th>
                    <th>书籍ISBN</th>
                    <th>书籍名称</th>
                    <th>书籍数量</th>
                    <th>进货地址</th>
                    <th>进货数量</th>
                </tr>
                </thead>
                <tbody>
                <c:forEach var="book" items="${requestScope.get('bookList')}">
                    <tr>
                        <input type="text" class="bookISBN" value="${book.getBookISBN()}" hidden>
                        <td><img src="${pageContext.request.contextPath}/book/readPhoto?bookISBN=${book.getBookISBN()}" width="40px" height="40px"></td>
                        <td class="bookISBN2">${book.getBookISBN()}</td>
                        <td class="bookName">${book.getBookName()}</td>
                        <td>${book.getBookCounts()}</td>
                        <td><input class="addr" type="text"></td>
                        <td><input class="nums" type="text"></td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
            <center><input type="button" class="btn btn-primary" onclick="p()" value="进货"></input></center>
        </div>
    </div>
    <br>
    <div align="center">
        <label>第${requestScope.page.getPageCurrent()}/${page.pageCount}页</label>
        <a href="${pageContext.request.contextPath}/admin/toStocking?pageNo=1">首页</a>
        <a href="${pageContext.request.contextPath}/admin/toStocking?pageNo=${page.pageCurrent-1}" >上一页</a>
        <a href="${pageContext.request.contextPath}/admin/toStocking?pageNo=${page.pageCurrent+1}" >下一页</a>
        <a href="${pageContext.request.contextPath}/admin/toStocking?pageNo=${page.pageCount}">尾页</a> 跳转到:
        <input type="text" style="width:30px" id="turnPage" />页
        <input type="button" onclick="startTurn()" value="跳转" />
    </div>


</div>

</body>
